package com.github.wxiaoqi.security.biz.modules.pay.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.biz.common.cms.request.CmsPayOrderQueryRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.pay.entity.CmsPayOrder;
import com.github.wxiaoqi.security.biz.modules.pay.mapper.CmsPayOrderMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 支付订单
 * @author he
 *
 */
@Service
public class CmsPayOrderBiz extends BaseBiz<CmsPayOrderMapper, CmsPayOrder> {

	private static final Logger log = (Logger) LoggerFactory.getLogger(CmsPayOrderBiz.class);

	/**
	 * 查询列表
	 * @param param
	 * @return
	 */
	public Map<String, Object> payOrderQuery(CmsPayOrderQueryRequest param) {
		log.info("支付订单-请求参数：{}" + EntityUtils.beanToMap(param));
		Map<String, Object> response = new HashMap<String, Object>();
		try {
			CmsPayOrder payOrderReq = new CmsPayOrder();
			payOrderReq.setPlatformId(param.getPlatformId());
			payOrderReq.setMerchantId(param.getMerchantId());
			payOrderReq.setCustomerId(param.getCustomerId());
			payOrderReq.setPersonalId(param.getPersonalId());
			payOrderReq.setPayCode("1");
			List<CmsPayOrder> payOrderList = mapper.select(payOrderReq);
			response.put("orderList",payOrderList);
			response.put("code", BizResponseCode.OK.getCode());
			response.put("message", BizResponseCode.OK.getMessage());
			log.info("支付订单 返回参数。。。。。。{}", response);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("支付订单  报错--"+e.getMessage());
			response.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		return response;
	}
	
	/**
	 * 支付订单列表-对内
	 * @param param
	 * @return
	 */
	public TableResultResponse<CmsPayOrder> payOrderList(CmsPayOrderQueryRequest param) {
		log.info("支付订单-请求参数：{}" + EntityUtils.beanToMap(param));
		Example example = new Example(CmsPayOrder.class);
		Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(param.getPlatformId())) {
			criteria.andEqualTo("platformId", param.getPlatformId());
		}
		if (!StringUtils.isEmpty(param.getMerchantId())) {
			criteria.andEqualTo("merchantId", param.getMerchantId());
		}
		if (!StringUtils.isEmpty(param.getCustomerId())) {
			criteria.andEqualTo("customerId", param.getCustomerId());
		}
		criteria.andEqualTo("payCode", "1");
		example.setOrderByClause("create_time DESC");
		Page<CmsPayOrder> result = PageHelper.startPage(param.getPage(), param.getLimit());
		List<CmsPayOrder> list = mapper.selectByExample(example);
		return new TableResultResponse<CmsPayOrder>(result.getTotal(), list);
	}
	
	
	/**
	 * 
	 * @return
	 */
	public void updateVerifyStatus() {
		log.info("定时任务修改支付订单过期状态。。。");
		try {
			mapper.updateVerifyStatus();
			log.info("定时任务修改支付订单过期状态success");
		} catch (Exception e) {
			log.info("定时任务修改支付订单过期状态异常"+e.getMessage());
		}
	}


}
